// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.states;

import java.util.HashMap;
import zombie.ai.State;
import zombie.audio.parameters.ParameterZombieState;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.IsoZombie;
import zombie.core.Core;
import zombie.core.Rand;
import zombie.core.skinnedmodel.advancedanimation.AnimEvent;
import zombie.gameStates.IngameState;
import zombie.iso.objects.RainManager;

public final class ZombieIdleState extends State {
    private static final ZombieIdleState _instance = new ZombieIdleState();
    private static final Integer PARAM_TICK_COUNT = 0;

    public static ZombieIdleState instance() {
        return _instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
        HashMap hashMap = owner.getStateMachineParams(this);
        ((IsoZombie)owner).soundSourceTarget = null;
        ((IsoZombie)owner).soundAttract = 0.0F;
        ((IsoZombie)owner).movex = 0.0F;
        ((IsoZombie)owner).movey = 0.0F;
        owner.setStateEventDelayTimer(this.pickRandomWanderInterval());
        if (IngameState.instance == null) {
            hashMap.put(PARAM_TICK_COUNT, 0L);
        } else {
            hashMap.put(PARAM_TICK_COUNT, IngameState.instance.numberTicks);
        }
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        HashMap hashMap = owner.getStateMachineParams(this);
        isoZombie.movex = 0.0F;
        isoZombie.movey = 0.0F;
        if (Core.bLastStand) {
            IsoPlayer isoPlayer = null;
            float _float = 1000000.0F;

            for (int int0 = 0; int0 < IsoPlayer.numPlayers; int0++) {
                if (IsoPlayer.players[int0] != null && IsoPlayer.players[int0].DistTo(owner) < _float && !IsoPlayer.players[int0].isDead()) {
                    _float = IsoPlayer.players[int0].DistTo(owner);
                    isoPlayer = IsoPlayer.players[int0];
                }
            }

            if (isoPlayer != null) {
                isoZombie.pathToCharacter(isoPlayer);
            }
        } else {
            if (((IsoZombie)owner).bCrawling) {
                owner.setOnFloor(true);
            } else {
                owner.setOnFloor(false);
            }

            long _long = (Long)hashMap.get(PARAM_TICK_COUNT);
            if (IngameState.instance.numberTicks - _long == 2L) {
                ((IsoZombie)owner).parameterZombieState.setState(ParameterZombieState.State.Idle);
            }

            if (!isoZombie.bIndoorZombie) {
                if (!isoZombie.isUseless()) {
                    if (isoZombie.getStateEventDelayTimer() <= 0.0F) {
                        owner.setStateEventDelayTimer(this.pickRandomWanderInterval());
                        int int1 = (int)owner.getX() + (Rand.Next(8) - 4);
                        int int2 = (int)owner.getY() + (Rand.Next(8) - 4);
                        if (owner.getCell().getGridSquare((double)int1, (double)int2, (double)owner.getZ()) != null
                            && owner.getCell().getGridSquare((double)int1, (double)int2, (double)owner.getZ()).isFree(true)) {
                            owner.pathToLocation(int1, int2, (int)owner.getZ());
                            isoZombie.AllowRepathDelay = 200.0F;
                        }
                    }

                    isoZombie.networkAI.mindSync.zombieIdleUpdate();
                }
            }
        }
    }

    @Override
    public void exit(IsoGameCharacter owner) {
    }

    @Override
    public void animEvent(IsoGameCharacter owner, AnimEvent event) {
    }

    private float pickRandomWanderInterval() {
        float _float = (float)Rand.Next(400, 1000);
        if (!RainManager.isRaining()) {
            _float *= 1.5F;
        }

        return _float;
    }
}
